﻿importPackage(Packages.client);
importPackage(Packages.client.inventory);
importPackage(Packages.server);
importPackage(Packages.tools);

var status = 0;
var DJ = 10000; //扣除的点卷
var 高等五彩水晶 = 4251202; //扣除的点卷
var ttt = "#fUI/UIWindow.img/Quest/icon9/0#";
var xxx = "#fUI/UIWindow.img/Quest/icon8/0#";
var sss = "#fUI/UIWindow.img/QuestIcon/3/0#";

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == -1) {
        cm.dispose();
    } else {
        if (status >= 0 && mode == 0) {
            cm.dispose();
            return;
        }
        if (mode == 1)
            status++;
        else
            status--;

 if (status == 0) {
    var textz = "#e#b装备回收系统,使用前请阅读说明: \r\n\r\n#k1.请将要回收装备放到背包第24格以后 \r\n#k2.根据装备属性返还#r6万-100万#k不等的冒险币!\r\n3.请看准了在回收,放错了装备后果自负!)\r\n";

        textz += "#r#L2#『回收背包第24格以后装备』\r\n";
    cm.sendSimple(textz);
} else if (status == 1) {
    if (selection == 1) { //材料强化在装备上
        if (!cm.getChar()
            .getInventory(MapleInventoryType.EQUIP)
            .getItem(24)
            .copy()
            .getUpgradeSlots() != 0) {
            cm.sendOk("你的装备强化次数不够");
            cm.dispose();
        }
    } else if (selection == 2) {
        var hasEquip = false;
        var hasCashEquip = false; //新增判断，记录是否有时装
        var equipList = ""; // 用于存储回收的装备列表
        for (var i = 24; i <= 96; i++) {
            if (cm.getChar().getInventory(MapleInventoryType.EQUIP).getItem(i) != null) {
                hasEquip = true;
                var item = cm.getChar().getInventory(MapleInventoryType.EQUIP).getItem(i).copy();
                var ii = MapleItemInformationProvider.getInstance();
                if (ii.isCash(item.getItemId())) {
                    hasCashEquip = true;
                    break;
                }
                equipList += "#i" + item.getItemId() + ":#  "; // 显示回收的装备图标
            }
        }
        if (!hasEquip) {
            cm.sendOk("背包里第24-96格没有装备不能回收！");
            cm.dispose();
            return;
        }
        if (hasCashEquip) { //如果有时装则直接提示并结束回收操作
            cm.sendOk("24-96格中存在时装，不能回收！");
            cm.dispose();
            return;
        }
        cm.sendYesNo("#r#e装备回收成功，共获得 " + calculateMesos(item) + " 冒险币！\r\n回收的装备列表:\r\n" + equipList + "\r\n\r\n是否确认回收？");
    }
} else if (status == 2) {
    var mesosGained = 0;
    for (var i = 24; i <= 96; i++) {
        if (cm.getChar().getInventory(MapleInventoryType.EQUIP).getItem(i) != null) {
            var item = cm.getChar().getInventory(MapleInventoryType.EQUIP).getItem(i).copy();
            MapleInventoryManipulator.removeFromSlot(
                cm.getC(),
                MapleInventoryType.EQUIP,
                i,
                1,
                true
            );
            var mesos = calculateMesos(item);
            mesosGained += mesos;
        }
    }
    cm.gainMeso(mesosGained);
    cm.sendOk("#r#e装备回收成功，共获得 " + mesosGained + " 冒险币！");
    cm.dispose();
}

}
}

function calculateMesos(item) {
  var level = item.getEnhance();
  if (level < 1) {
    level = 1;
  }
  // 生成一个 20000-170000 之间的随机整数，作为金币数量
  var randomMesos = Math.floor(Math.random() * (1000 - 10000 + 1)) + 50000;
  return level * randomMesos; // 根据随机金币数量计算返还的金币数量
}